﻿using System;
using System.Configuration;
using System.Linq;
using System.Web.Mvc;

using Microsoft.Web.Infrastructure.DynamicModuleHelper;

using StackExchange.Profiling.MVCHelpers;
using StackExchange.Profiling;

[assembly: System.Web.PreApplicationStartMethod(typeof(MVCBootstrap.Profiling.MiniProfilerInitializer), "PreStart")]

namespace MVCBootstrap.Profiling {

	public class MiniProfilerInitializer {

		public static void PreStart() {
			if (ConfigurationManager.AppSettings["MiniProfiler"] != "true") {
				return;
			}

			// TODO: Get rid of this!! This is strictly db provider code!
			MiniProfilerEF.Initialize(false);

			//Make sure the MiniProfiler handles BeginRequest and EndRequest
			DynamicModuleUtility.RegisterModule(typeof(MiniProfilerStartupModule));

			//Setup profiler for Controllers via a Global ActionFilter
			GlobalFilters.Filters.Add(new ProfilingActionFilter());

			var copy = ViewEngines.Engines.ToList();
			ViewEngines.Engines.Clear();
			foreach (var item in copy) {
				ViewEngines.Engines.Add(new ProfilingViewEngine(item));
			}
		}
	}
}